package com.example.application8;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link com.example.application8.Fragment3#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment3 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public Fragment3() {
        // Required empty public constructor
        super(R.layout.fragment_3);
    }

//    @Override
//    public void onViewCreated(@NonNull View view, @NonNull Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        //写入外部存储
//        Button button3 = getView().findViewById(R.id.button3);
//        button3.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                File externalPath = getActivity().getExternalFilesDir(null);
//                File file = new File(externalPath,"Mydata2");
//                try {
//                    FileOutputStream os = new FileOutputStream(file);
//                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
//                    writer.write("Tom,I thank you");
//                    writer.close();
//                    Toast.makeText(getActivity(),"写入成功",Toast.LENGTH_SHORT).show();
//                } catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        });
//
//        //读取外部存储
//        Button button4 = getView().findViewById(R.id.button4);
//        button4.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                File externalPath = getActivity().getExternalFilesDir(null);
//                File file = new File(externalPath,"Mydata2");
//                try {
//                    FileInputStream is = new FileInputStream(file);
//                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//                    String line = reader.readLine();
//                    Toast.makeText(getActivity(),line,Toast.LENGTH_SHORT).show();
//                    is.close();
//                } catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment3.
     */
    // TODO: Rename and change types and number of parameters
    public static com.example.application8.Fragment3 newInstance(String param1, String param2) {
        com.example.application8.Fragment3 fragment = new com.example.application8.Fragment3();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_3, container, false);
    }
}